<template>
  <view class="page-wrap">
    <view v-if="!showSuccess" class="verify-wrapper">
      <p class="title">请选择企业</p>
      <view v-if="sitelist.length === 0" class="tip">
        当前用户暂无可加入企业
      </view>
      <view v-else class="item-list">
        <view v-for="(item, index) in sitelist" :key="index" class="enterprise-item" @tap="selectedSite(index, item)">
          <view class="item-left">
            <span>{{ item.name }}</span>
          </view>
          <view class="item-right">
            <!-- <text class="iconfont">&#xe65a;</text> -->
            <image
              v-if="selectIndex === index"
              style="width: 20px;height: 20px;"
              src="/authorize/img/selected.png"
              mode="widthFix"
            />
          </view>
        </view>
      </view>
      <view class="btn-wxphone bgFCAF4F" :disabled="!clickable" @click="confirmJoin">
        确定加入
      </view>
    </view>
    <view v-else class="success-wrapper">
      <image
        src="/authorize/img/wancheng.png"
        mode="widthFix"
      />
      <p>您已成功绑定</p>
      <p>{{ currentSelectedSite.name }}</p>
      <view class="btn-wxphone borderFCAF4F mt64" @click="goUse">
        前往使用
      </view>
    </view>
  </view>
</template>
<script>

import { getSiteListVOByPhone, bindWeChatFromOfficialAccount } from '@/services/register'
export default {
  name: '',
  components: {},
  props: {},
  data(){
    return {
      siteId: null,
      unionId: null,
      openId: null,
      phone: null,
      selectIndex: -1,
      currentSelectedSite: {},
      sitelist: [],
      clickable: false,
      showSuccess: false
    }
  },
  watch: {},
  onLoad(options){
    this.phone = options.phone
    this.unionId = options.unionId
    this.openId = options.openId
    this.getSiteListVOByPhone(options.phone)
  },
  mounted(){},
  methods: {
    getSiteListVOByPhone(phone){
      getSiteListVOByPhone({ phone: phone }).then(res => {
        this.sitelist = res
      }).catch(err => {
        uni.showToast({
          title: err,
          icon: 'none',
          duration: 2000
        })
      })
    },
    goUse(){
      uni.removeStorageSync('token')
      uni.redirectTo({
        url: '/pages/index/index'
      })
    },
    confirmJoin(){
      var weChatBindFromOffAccountDTO = {
        phone: this.phone,
        unionId: this.unionId,
        openId: this.openId,
        siteId: this.siteId,
        thirdInfoDO: {
        }
      }
      if(this.siteId){
        bindWeChatFromOfficialAccount(weChatBindFromOffAccountDTO).then(res => {
          if(res.code === 400){
            uni.showToast({
              title: res.msg,
              icon: 'none',
              duration: 2000
            })
            uni.removeStorageSync('token')
            uni.reLaunch({
              url: '/pages/index/index'
            })
          }else{
            this.showSuccess = true
          }
        }).catch(err => {
          uni.showToast({
            title: err.data.msg,
            icon: 'none',
            duration: 2000
          })
        })
      }else{
        uni.showToast({
          title: '请选择用工单位',
          icon: 'none',
          duration: 2000
        })
      }
    },
    goBack(){
      uni.redirectTo({
        url: '/pages/index/index'
      })
    },
    selectedSite(index, item){
      this.currentSelectedSite = item
      this.siteId = item.id
      this.clickable = true
      this.selectIndex = index
    }
  }
}
</script>
